﻿using EventSubscriptionExample;
using Interview.Model.Enum;
using Interview.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Interview.Model
{
    public abstract class ElectricalBase : IElectricalBase
    {
        public string Name { get; set; }

        public ElectricalBase(string Name)
        {
            this.Name = Name;
        }

        private ElectricalStatusEnum _status;
        public ElectricalStatusEnum Status
        {
            get => _status;
            set
            {
                if (_status != value)
                {
                    ElectricalStatusEnum oldStatus = _status;
                    _status = value;
                    // 触发状态变化事件
                    OnStatusChanged(oldStatus, value);
                }
            }
        }

        public virtual void TurnOn() { }
        public virtual void TurnOff() { }
        public void CheckStatus()
        {
            if (this.Status == ElectricalStatusEnum.Err)
            {
                Console.WriteLine($"{Name} 故障!");

            }
        }


        public event EventHandler<ElectricalStatusEventArgs> StatusChanged;


        public virtual void OnStatusChanged(ElectricalStatusEnum oldStatus, ElectricalStatusEnum newStatus)
        {
            this.Status = newStatus;

            StatusChanged?.Invoke(this, new ElectricalStatusEventArgs(Name, oldStatus, newStatus));

            // 如果电器损坏，触发警报
            if (newStatus == ElectricalStatusEnum.Err)
            {
                WarningService.Instance.Alert($"{Name} 损坏了!");
            }
        }

        


    }
}
